/*
 * SomeXStatusForm.java
 *
 * Created on 7 Август 2011 г., 13:35
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
// #sijapp cond.if modules_XSTATUSES is "true" #
package jimm.forms;

import jimm.cl.ContactList;
import jimm.comm.StringConvertor;
import jimm.io.Storage;
import jimm.ui.menu.*;
import protocol.*;
import protocol.icq.*;
import protocol.jabber.*;
import protocol.mrim.*;

/**
 *
 * @author Vladimir Kryukov
 */
public abstract class SomeXStatusForm implements SelectListener {
    private MenuModel menu = new MenuModel();
    protected Protocol protocol;
    protected int xstatus;
    
    protected String[] xst_titles = new String[100];
    protected String[] xst_descs  = new String[100];

    public SomeXStatusForm(Protocol protocol) {
        this.protocol = protocol;
        load();
    }
    public final void select(Select select, MenuModel model, int statusIndex) {
        xstatus = statusIndex;
        if (-1 == statusIndex) {
            setXStatus("", "");
            back();

        } else {
            onStatusSelected();
        }
    }
    private String getProtocolId() {
        // #sijapp cond.if protocols_ICQ is "true" #
        if (protocol instanceof Icq) {
            return "icq";
        }
        // #sijapp cond.end#
        // #sijapp cond.if protocols_MRIM is "true" #
        if (protocol instanceof Mrim) {
            return "mrim";
        }
        // #sijapp cond.end#
        // #sijapp cond.if protocols_JABBER is "true" #
        if (protocol instanceof Jabber) {
            return "jabber";
        }
        // #sijapp cond.end#
        return "";
    }
    
    private void load() {
        try {
            Storage storage = new Storage(getProtocolId() + "-xstatus");
            storage.open(false);
            storage.loadXStatuses(xst_titles, xst_descs);
            storage.close();
        } catch (Exception e) {
        }
    }
    
    public final void show() {
        menu.clean();
        XStatusInfo info = protocol.getXStatusInfo();
        for (byte i = -1; i < info.getXStatusCount(); ++i) {
            menu.addItem(info.getName(i), info.getIcon(i), i);
        }
        menu.setDefaultItemCode(protocol.getProfile().xstatusIndex);
        menu.setActionListener(this);
        new Select(menu).show();
    }
    public final void back() {
        ContactList.getInstance().activate();
    }

    
    protected final void setXStatus(String title, String desc) {
        if (0 <= xstatus) {
            xst_titles[xstatus] = StringConvertor.notNull(title);
            xst_descs[xstatus]  = StringConvertor.notNull(desc);
            try {
                Storage storage = new Storage(getProtocolId() + "-xstatus");
                storage.open(true);
                storage.saveXStatuses(xst_titles, xst_descs);
                storage.close();
            } catch (Exception e) {
            }
        }
        protocol.setXStatus(xstatus, title, desc);
        ContactList.getInstance().updateMainMenu();
    }
    protected abstract void onStatusSelected();
}
// #sijapp cond.end #
